package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsAdminStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsAdminStorageServiceImpl implements DtsAdminStorageService {

    @Autowired
    DtsStorageMapper storageMapper;

    @Override
    public IPage<DtsStorage> queryPage(Integer page, Integer limit, String sort, String order, String key, String name) {

        IPage<DtsStorage> ipage = new Page<>(page, limit);

        QueryWrapper<DtsStorage> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            qw.eq("`key`", key);
        }
        if(!StringUtils.isEmpty(name)){
            qw.eq("name", name);
        }

        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        storageMapper.selectPage(ipage, qw);
        return ipage;
    }

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public DtsStorage add(byte[] content,String key, String filename,int size, String type) throws Exception {

        //上传图片到阿里云oss
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, filename, content);
        System.out.println(url);
        //保存对象到数据库中
        DtsStorage storage = new DtsStorage();
        storage.setUpdateTime(new Date());
        storage.setAddTime(new Date());
        storage.setDeleted(false);
        storage.setName(filename); //原始文件名
        storage.setKey(key); //唯一的名称
        storage.setSize(size);
        storage.setType(type);
        storage.setUrl(url);
        storageMapper.insert(storage);

        return storage;
    }

    @Override
    public void delete(DtsStorage storage) {
        //删除数据库的记录
        storageMapper.deleteById(storage.getId());
        //删除阿里云oss上的图片数据
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId, accessKeySecret, bucketName, storage.getKey());

    }

    @Override
    public void update(DtsStorage storage) {
        //如果要修改图片：
        // 1. 要删除原来存储在阿里云oss上的图片
        // 2. 新增图片到阿里云oss
        // 3. 数据库storage对象也要修改

        storageMapper.updateById(storage);
    }
}
